﻿
/*
 *
 * Copyright (C) 2009 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System.IO;
using System.Xml;
using FlowLib.Containers;
using ClientExample.Containers;
using ClientExample.Comparers;

namespace ClientExample.Utils
{
    public class FriendsXml : XmlList<Friend>
    {
        public FriendsXml()
        {
            TopNodeName = "Friends";
            Items = new FlowSortedList<Friend>(new FriendComparer(UserInfoComparer.Columns.StoreId));
        }

        public override void Read(byte[] data)
        {
            base.Read(data);
        }

        protected override void NodeInfo(Friend item, bool isSave)
        {
            if (isSave)
            {
                NewNode("Friend", isSave);
                NodeInfo("StoreID", item.StoreID, isSave);
                NodeInfo("DisplayName", item.DisplayName, isSave);
                NodeInfo("LastSeen", item.LastSeen.ToString(), isSave);
                EndNode(isSave);
            }
        }

        protected override void NodeInfo(string key, string value, bool isSave)
        {
            if (!isSave)
            {
                switch (key)
                {
                    case "StoreID":
                        itemCurrent.Set(UserInfo.STOREID, value);
                        break;
                    case "DisplayName":
                        itemCurrent.DisplayName = value;
                        break;
                    case "LastSeen":
                        try
                        {
                            itemCurrent.LastSeen = long.Parse(value);
                        }
                        catch { }
                        break;
                }
            }
            else
            {
                base.NodeInfo(key, value, isSave);
            }
        }
    }
}
